package com.qianfeng.openplatform.zuul.filter;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import com.qianfeng.openplatform.common.BaseResultBean;
import com.qianfeng.openplatform.common.CommonsConstants;
import com.qianfeng.openplatform.common.SystemParams;
import com.qianfeng.openplatform.zuul.feign.CacheFeign;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * @author LJF
 * @date 2020/8/7 19:26
 * @version 1.0
 */
@Component
public class IdempotentsFilter extends ZuulFilter {

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private CacheFeign cacheFeign;

    @Override
    public String filterType() {
        return FilterConstants.PRE_TYPE;
    }

    @Override
    public int filterOrder() {
        return 40;
    }

    @Override
    public boolean shouldFilter() {
        RequestContext currentContext = RequestContext.getCurrentContext();
        HttpServletRequest request = currentContext.getRequest();
        String method = request.getParameter("method");
        boolean isIdempotents = true;
        try {
            String idempotents = cacheFeign.hGet(SystemParams.METHOD_REDIS_PRE + method, "idempotents");
            isIdempotents = "1".equals(idempotents);
        } catch (Exception exception) {
            exception.printStackTrace();
        }

        return currentContext.sendZuulResponse() && isIdempotents;
    }

    @SneakyThrows
    @Override
    public Object run() throws ZuulException {

        //获取到用户传递的签名
        RequestContext currentContext = RequestContext.getCurrentContext();
        HttpServletRequest request = currentContext.getRequest();
        String sign = request.getParameter("sign");

        //去我们保存的位置看签名是否存在
        String result = cacheFeign.getFromRedis(SystemParams.IDEMPOTENTS_REDIS_PRE + sign);
        if (result != null){
            //代表数据存在，拦截请求
            currentContext.setSendZuulResponse(false);
            BaseResultBean resultBean = new BaseResultBean();
            resultBean.setMsg("请勿重复提交！");
            resultBean.setCode(CommonsConstants.IDEMPOTENTS_ERROR);
            String json = null;
            try {
                json = objectMapper.writeValueAsString(resultBean);
            }catch (JsonProcessingException jsonProcessingException){
                jsonProcessingException.printStackTrace();
            }

            currentContext.getResponse().setContentType("application/json;charset=utf-8");
            currentContext.setResponseBody(json);
            return null;
        }else {
            cacheFeign.save2Redis(SystemParams.IDEMPOTENTS_REDIS_PRE+sign,System.currentTimeMillis()+"",600);
            return null;
        }

    }
}
